package com.xiaoshi.system.controller;


import com.xiaoshi.system.dto.SysDictTypeDto;
import com.xiaoshi.system.qo.SysDictTypeQo;
import com.xiaoshi.system.service.ISysDictTypeService;
import com.xiaoshi.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;


import javax.validation.Valid;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author yuankai
 * @since 2022-03-24
 */
@Api(tags = "字典类型管理器")
@RestController
@RequestMapping("/system/sys-dict-type")
public class SysDictTypeController {
    @Autowired
    private ISysDictTypeService sysDictTypeService;

    @ApiOperation("获取字典分页列表信息")
    @GetMapping("/getPage")
    public JsonResult getPage(SysDictTypeQo qo) {
        return JsonResult.success(sysDictTypeService.getPage(qo));
    }

    @ApiOperation("添加字典")
    @PostMapping("/add")
    public JsonResult addSysDictType(@Valid @RequestBody SysDictTypeDto sysDictTypeDto, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return JsonResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        if(sysDictTypeService.addSysDictType(sysDictTypeDto)){
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @ApiOperation("编辑字典")
    @PostMapping("/edit")
    public JsonResult editSysDictType(@RequestBody SysDictTypeDto sysDictTypeDto) {

        if(sysDictTypeService.editSysDictType(sysDictTypeDto)){
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @ApiOperation("根据名称获取")
    @GetMapping("/getByName")
    public JsonResult getByName(String name){
        return JsonResult.success(sysDictTypeService.getByName(name));
    }

    @ApiOperation("根据ID 删除")
    @GetMapping("/delById")
    public JsonResult delById(@RequestParam Long id){
        return JsonResult.success(sysDictTypeService.delById(id));
    }
}
